import { $wx } from '@/apis/user'
import { getOptions } from '@/utils/index'

App({
  async onLaunch ({ query }) {
    await this.getSystemInfo();
    await this.loginWeixin()
    /// await this.getAppInfo();
    if (this.appReadyCallback) this.appReadyCallback(this.globalData);
    await this.shareUrl(query);
    console.log('[globalData]', this.globalData);
  },
  // 系统信息
  getSystemInfo (cb) {
    this.globalData.sysInfo = wx.getSystemInfoSync();
  },
  // 登陆微信
  loginWeixin () {
    return new Promise((resolve, reject) => {
      const that = this;
      wx.login({
        success: async ({ code }) => {
          const { result } = await $wx.wxLogin({ code })
          console.log('$wx.wxLogin ==> ', result);
          const { token, info, mp, setting } = result;
          that.globalData.userInfo = { info, mp, setting };
          that.globalData.token = token;
          if (this.userInfoReadyCallback) this.userInfoReadyCallback(result);
          resolve()
        },
      })
    });
  },
  // APP信息
  async getAppInfo () {
    const { result } = await $wx.getAppInfo({
      appid: 1057
    });
    const { abbrevia, aldKey, logo, platform } = result;
    this.globalData.appInfo = { abbrevia, aldKey, logo, platform };
    if (this.appInfoReadyCallback) this.appInfoReadyCallback(result)
  },
  // 分享转发
  shareUrl (query) {
    const options = getOptions(query);
    if (!query.scene) return false;
    const { q } = options;
    if (q == 1) {
      wx.navigateTo({
        url: '/package/m1/pages/test/index'
      });
    }
  },
  // 全局数据
  globalData: {
    sysInfo: {},
    userInfo: {},
    token: '',
    appInfo: {}
  }
});
